<template>
  <div class="list">
    <div class="content">
      <div class="crumbs">
        <a @click="$router.push('/')" class="item-a">首页</a>
        <span class="arrow">></span>
        <span class="item-span">农产品列表</span>
      </div>
      <div class="goods-sort">
        <div class="sort-con categorys">
          <div class="name">分类：</div>
          <div class="group">
            <span
              class="item"
              :class="{ active: currentCategoryIndex == index}"
              v-for="(item, index) in categoryData"
              @click="changeCategoryIndex(index,item._id)"
              :key="item._id"
              >{{ item.name }}
            </span>
          </div>
        </div>
        <div class="sort-con sorts">
          <div class="name">排序：</div>
          <div class="group">
            <span
              class="item"
              :class="{ active: currentSortIndex == index}"
              @click="changeSortIndex(index)"
              v-for="(item, index) in sortData"
              >{{ item.name }}</span
            >
          </div>
        </div>
      </div>
      <div class="goods-list">
        <div class="list-con">
          <div class="item" v-for="item in productData" @click="goDetail(item._id)">
            <div class="goods-img">
              <img :src="item.productImage" alt="" />
            </div>
            <div class="goods-msg">
              <div class="goods-name">{{item.productName}}{{ item.description }}</div>
              <div class="goods-price">
                <div class="price">￥<span class="medium">{{item.price}}</span></div>
              </div>
            </div>
          </div>
        </div>
        <div class="pagination">
          <el-pagination
            background
            v-model:current-page="pageNo"
            v-model:page-size="pageSize"
            v-model:total="total"
            layout="prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="goodsList">
import { ref, onMounted} from "vue";
import { useRouter, useRoute } from "vue-router";
import { reqCategoryList } from "@/api/category";
import {reqProductList} from "@/api/product"
import { ElMessage } from "element-plus";

//获取路由器对象
let $router = useRouter();
let $route = useRoute();
// 分类索引
let currentCategoryIndex = ref(0);
let currentSortIndex = ref(0);
let sortData = ref<any>([{ name: "默认" }, { name: "价格" }]);
// 分类数据
let categoryData = ref<any>([]);
// 农产品数据
let productData = ref<any>([])
// 分页器数据
let pageNo = ref(1);
let pageSize = ref(10);
let total = ref(0);
let cid = $route.query.cid
// 分类
let choseCid = ref('')

// 点击跳转到商品详情页
const goDetail = (_id:string) => {
  $router.push({
    path:`/detail/${_id}`,
    query:{
      product:1
    }
  });
};
// 点击修改索引添加动态样式
const changeCategoryIndex = (index: number,cid:string) => {
  choseCid.value = cid
  currentCategoryIndex.value = index;
  pageNo.value = 1
  getGoodsList(pageNo.value,pageSize.value,currentSortIndex.value,choseCid.value)
};
const changeSortIndex = (index: number) => {
  currentSortIndex.value = index;
  pageNo.value = 1
  getGoodsList(pageNo.value,pageSize.value,currentSortIndex.value,choseCid.value)
};

// 分页器回调
const handleSizeChange = (value: number) => {
  pageNo.value = value;
};
const handleCurrentChange = (value: number) => {
  pageNo.value = value;
};

// 获取一级分类列表
const getCategoryList = async () => {
  let cid = $route.query.cid
  let result = await reqCategoryList();
  if (result.code == 200) {
    categoryData.value = result.data;
    let index = categoryData.value.findIndex((item:any) => {
      if(item._id == cid){
        return true
      }
    })
    currentCategoryIndex.value = ++index
    result.data.unshift({ name: "全部" });
  }
};

// 获取农产品数据
const getGoodsList = async (pageNo:number,pageSize:number,sort:number,cid:any) => {
  let result = await reqProductList(pageNo,pageSize,sort,cid || '')
  if(result.code == 200){
    productData.value = result.data
    total.value = result.total
  }else{
    ElMessage.error(result.msg)
  }
}

onMounted(() => {
  getCategoryList();
  getGoodsList(pageNo.value,pageSize.value,currentSortIndex.value,cid)
});
</script>

<style lang="scss" scoped>
.list {
  padding-bottom: 40px;
  min-height: 535px;
  background: #f5f5f5;
  .content {
    width: 1200px;
    margin: auto;
    .goods-sort {
      background: #fff;
      padding: 0 20px;
      margin-bottom: 20px;
      font-size: 12px;
      .sort-con {
        display: flex;
        padding: 20px 0 0 10px;
        border-bottom: 1px dashed #ddd;
        .name {
          color: #999;
          margin-right: 20px;
        }
        .group {
          flex: 1;
          margin-bottom: 4px;
          .item {
            display: inline-block;
            margin: 0 30px 16px 0;
            transition: all 0.2s;
            -webkit-transition: all 0.2s;
            cursor: pointer;
            &:hover {
              color: #4caf50;
            }
          }
          .active {
            color: #4caf50;
          }
        }
      }
      .sort-con:last-child {
        display: flex;
        padding: 20px 0 0 10px;
        border: 0;
      }
    }
    .crumbs {
      font-size: 12px;
      line-height: 1;
      padding: 23px 0 20px;
      .item-a {
        cursor: pointer;
        &:hover {
          text-decoration: underline;
        }
      }
      .arrow {
        font-family: Consolas;
        margin: 0 2px;
      }
      .item-span {
        color: #999;
      }
    }
    .goods-list {
      padding-bottom: 30px;
      .list-con {
        display: flex;
        flex-wrap: wrap;
        min-height: 440px;
        .item {
          position: relative;
          width: 224px;
          height: 298px;
          background: #fff;
          margin-top: 20px;
          margin-right: 20px;
          transition: all 0.2s;
          cursor: pointer;
          &:hover {
            box-shadow: 0 0 16px 0 hsla(0, 0%, 60%, 0.2);
          }
          &:nth-child(5n) {
            margin-right: 0;
          }
          .goods-img {
            width: 160px;
            height: 160px;
            margin: 30px 32px 18px;
            text-align: center;
            font-size: 0;
            img {
              width: 160px;
              height: 160px;
              vertical-align: middle;
            }
          }
          .goods-msg {
            padding: 0 23px 10px;
            .goods-name {
              height: 40px;
              line-height: 20px;
              font-size: 14px;
              max-width: 100%;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
            }
            .goods-price {
              display: flex;
              margin-top: 10px;
              overflow: hidden;
              .price {
                font-family: arial;
                font-size: 12px;
                color: #e1251b;
                display: flex;
                align-items: baseline;
                .medium {
                  font-size: 22px;
                }
              }
            }
          }
        }
      }
      .pagination {
        margin-top: 30px;
        display: flex;
        justify-content: flex-end;
        text-align: center;
      }
    }
  }
}
</style>
